﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using VisualWip.Core.Interfaces;
using VisualWip.Core.Utils;

namespace VisualWip.Core.Columns
{
    public abstract class ColumnBase : INotifyPropertyChanged, IPersistable
    {
        private string _columnName;
        private ColumnType _columnType;
        private int _height;
        private int _positionX;
        private int _positionY;
        private int _width;

        private string _windowName;
        private int _zIndex;

        protected ColumnBase()
        {
            Height = 800;
            Width = 300;
            PositionY = 0;
            ColumnType = ColumnType.Other;
        }

        public string WindowName
        {
            get { return _windowName; }
            set
            {
                if (value != _windowName)
                {
                    _windowName = value;
                    OnPropertyChanged(this, () => WindowName);
                }
            }
        }

        public int PositionX
        {
            get { return _positionX; }
            set
            {
                if (value != _positionX)
                {
                    _positionX = value;
                    OnPropertyChanged(this, () => PositionX);
                }
                Debug.WriteLine("Position X: {0}", _positionX);
            }
        }

        public int PositionY
        {
            get { return _positionY; }
            set
            {
                if (value != _positionY)
                {
                    _positionY = value;
                    OnPropertyChanged(this, () => PositionY);
                }
                Debug.WriteLine("Position Y: {0}", _positionY);
            }
        }

        public int Width
        {
            get { return _width; }
            set
            {
                if (value > 0)
                    if (value != _width)
                    {
                        _width = value;
                        OnPropertyChanged(this, () => Width);
                    }
                Debug.WriteLine("Width: {0}", _width);
            }
        }

        public int Height
        {
            get { return _height; }
            set
            {
                if (value > 0)
                    if (value != _height)
                    {
                        _height = value;
                        OnPropertyChanged(this, () => Height);
                    }
                Debug.WriteLine("Height: {0}", _height);
            }
        }


        public int ZIndex
        {
            get { return _zIndex; }
            set
            {
                if (value != _zIndex)
                {
                    _zIndex = value;
                    OnPropertyChanged(this, () => ZIndex);
                }
            }
        }

        public string ColumnName
        {
            get { return _columnName; }
            set
            {
                if (value != _columnName)
                {
                    _columnName = value;
                    OnPropertyChanged(this, () => ColumnName);
                }
            }
        }

        public ColumnType ColumnType
        {
            get { return _columnType; }
            set
            {
                if (value != _columnType)
                {
                    _columnType = value;
                    OnPropertyChanged(this, () => ColumnType);
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IPersistable Members

        public virtual PersistenceTree Persist(string name)
        {
            var settings = new PersistenceTree(name, this);
            settings.Add(() => ColumnName);
            settings.Add(() => PositionX);
            settings.Add(() => PositionY);
            settings.Add(() => Width);
            settings.Add(() => Height);
            settings.Add(() => WindowName);
            settings.AddChild(() => ColumnType);
            return settings;
        }

        public virtual void Load(PersistenceTree settings)
        {
            ColumnName = settings.Get(() => ColumnName, String.Empty);
            PositionX = settings.Get(() => PositionX, 0);
            PositionY = settings.Get(() => PositionY, 0);
            Width = settings.Get(() => Width, 5);
            Height = settings.Get(() => Height, 5);
            WindowName = settings.Get(() => WindowName, Window.MainWindowName);
            ColumnType = settings.GetChild(() => ColumnType);
        }

        #endregion

        protected void OnPropertyChanged<T>(object sender, Expression<Func<T>> property)
        {
            PropertyHelper.OnPropertyChanged(this, PropertyChanged, property);
        }
    }
}